/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package consys.social.core;

import consys.social.memcached.dao.spymemcached.Memcached;
import consys.social.memcached.dao.spymemcached.SpyMemcachedImpl;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

/**
 * install and uninstall tables and memcached settings without spring injection
 * with default settings
 *
 * TODO:NEEDS TO REFACTOR COMMONS
 *
 * @author Petr Kunc
 */
public class InstallatorNoSpring {

    public void install() throws MasterNotRunningException {
        String[] servers = {"localhost:11211"};
        Memcached memcached = new SpyMemcachedImpl("binary", servers);
        memcached.clear();
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.master", "localhost:60000");
        HBaseAdmin hbase = null;
        try {
            hbase = new HBaseAdmin(conf);
        } catch (ZooKeeperConnectionException ex) {
            System.out.println("nelze pripojit");
        }

        try {
            hbase.disableTable("entities");
            hbase.deleteTable("entities");
        } catch (IOException e) {
        }
        HTableDescriptor entTable = new HTableDescriptor("entities");
        HColumnDescriptor followers = new HColumnDescriptor("followers".getBytes());
        HColumnDescriptor following = new HColumnDescriptor("following".getBytes());
        HColumnDescriptor blocked = new HColumnDescriptor("blocked".getBytes());
        HColumnDescriptor news = new HColumnDescriptor("news".getBytes());
        HColumnDescriptor count = new HColumnDescriptor("count".getBytes());
        //HColumnDescriptor wall = new HColumnDescriptor("wall".getBytes());
        followers.setMaxVersions(1);
        following.setMaxVersions(1);
        count.setMaxVersions(1);
        entTable.addFamily(followers);
        entTable.addFamily(following);
        entTable.addFamily(count);
        entTable.addFamily(blocked);
        entTable.addFamily(news);
        //entTable.addFamily(wall);
        try {
            hbase.createTable(entTable);
        } catch (IOException ex) {
            System.out.println("nelze vytvorit");
        }
        try {
            hbase.disableTable("walls");
            hbase.deleteTable("walls");
        } catch (IOException e) {
        }
        HTableDescriptor wallTable = new HTableDescriptor("walls");
        HColumnDescriptor info = new HColumnDescriptor("info".getBytes());
        HColumnDescriptor text = new HColumnDescriptor("text".getBytes());
        HColumnDescriptor likes = new HColumnDescriptor("likes".getBytes());
        wallTable.addFamily(info);
        wallTable.addFamily(text);
        wallTable.addFamily(likes);
        try {
            hbase.createTable(wallTable);
        } catch (IOException ex) {
            System.out.println("nelze vytvorit wall");
        }

        try {
            hbase.disableTable("discussions");
            hbase.deleteTable("discussions");
        } catch (IOException e) {
        }
        HTableDescriptor discTable = new HTableDescriptor("discussions");
        discTable.addFamily(info);
        discTable.addFamily(text);
        try {
            hbase.createTable(discTable);
        } catch (IOException ex) {
            System.out.println("nelze vytvorit disk");
        }
    }

    public void uninstall() throws MasterNotRunningException, ZooKeeperConnectionException {
        String[] servers = {"localhost:11211"};
        Memcached memcached = new SpyMemcachedImpl("binary", servers);
        memcached.clear();
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.master", "localhost:60000");
        HBaseAdmin hbase = new HBaseAdmin(conf);
        try {
            hbase.disableTable("entities");
            hbase.deleteTable("entities");
        } catch (IOException ex) {
        }
        try {
            hbase.disableTable("walls");
            hbase.deleteTable("walls");
        } catch (IOException ex) {
        }
        try {
            hbase.disableTable("discussions");
            hbase.deleteTable("discussions");
        } catch (IOException ex) {
        }
    }
}
